package com.easy.work.function.mall.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.easy.work.R;

import java.util.HashMap;
import java.util.List;

/**
 * @author zhangfeng
 * @date 2017/12/24
 * @desc
 */

public class WelfareAdapter extends ArrayAdapter<String> {

    private List<String> mList;
    private LayoutInflater mInflater;

    public HashMap<Integer, String> getMap() {
        return map;
    }

    private HashMap<Integer, String> map = new HashMap<>();

    public WelfareAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
        mList = objects;
        mInflater = LayoutInflater.from(context);
        setDefaultState();
    }

    private void setDefaultState() {
        for (int i = 0; i < mList.size(); i++) {
            map.put(i, "");
        }
    }


    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_job_select, parent, false);
            holder = new ViewHolder();
            holder.tvOption = (TextView) convertView.findViewById(R.id.tv_option);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvOption.setText(mList.get(position));
        if ("1".equals(map.get(position))) {
            holder.tvOption.setSelected(true);
        } else {
            holder.tvOption.setSelected(false);
        }
        return convertView;
    }

    public void reset() {
        setDefaultState();
        notifyDataSetChanged();
    }

    public void updateSelect(int pos) {
        String i = "1".equals(map.get(pos)) ? "0" : "1";
        map.put(pos, i);
        notifyDataSetChanged();
    }

    class ViewHolder {
        TextView tvOption;
    }

}
